
package drugdealer.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;

import drugdealer.negocio.Facade;
import drugdealer.negocio.IFacade;

public abstract class SkelletonApp extends Composite {
	protected IFacade facade;

	protected CTabFolder folder;

	protected Button saveBtn;
	protected Button discardBtn;
	
	protected Image drugdealerImg;
	public Image newImg;
	public Image editImg;
	public Image deleteImg;
	public Image searchImg;
	public Image netImg;
	public Image localImg;
	public Image saveImg;
	public Image discardImg;
	
	/**
	 * @param parent
	 * @param categories
	 */
	public SkelletonApp(Composite parent) {
		super(parent, SWT.NONE);
		this.setLayout(parent.getLayout());

		this.loadImages();
		
		this.createSidebar(this);
		this.createBody(parent);
		this.createButtons(parent);
		
		this.facade = (IFacade) new Facade();
	}
	
	/**
	 * 
	 */
	private void loadImages() {
		this.drugdealerImg = new Image(this.getDisplay(), "./imgs/face-monkey.png");
		
		this.newImg = new Image(this.getDisplay(), "./imgs/window-new.png");
		this.editImg = new Image(this.getDisplay(), "./imgs/accessories-text-editor.png"); 
		this.deleteImg = new Image(this.getDisplay(), "./imgs/edit-delete.png");
		this.searchImg = new Image(this.getDisplay(), "./imgs/system-search.png");
		this.netImg = new Image(this.getDisplay(), "./imgs/applications-internet.png");
		this.localImg = new Image(this.getDisplay(), "./imgs/go-home.png");
		
		this.saveImg = new Image(this.getDisplay(), "./imgs/document-save.png");
		this.discardImg = new Image(this.getDisplay(), "./imgs/user-trash-full.png");
	}
	
	/**
	 * @param parent
	 */
	protected abstract void createSidebar(Composite parent);
	
	/**
	 * @param parent
	 * @return
	 */
	protected ExpandBar getSidebar(Composite parent) {
		ExpandBar bar = new ExpandBar (parent, SWT.NONE);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd.minimumWidth = 200;
		bar.setLayoutData(gd);
		bar.setSpacing(8);
		
		return bar;
	}
	
	/**
	 * @param composite
	 * @param bar
	 * @param title
	 * @param height
	 */
	protected void addSidebarItem(Composite composite, ExpandBar bar, 
			String title, int height) {
		ExpandItem xItem = new ExpandItem (bar, SWT.NONE, 0);
		xItem.setText(title);
		xItem.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		xItem.setHeight(height);
		xItem.setControl(composite);
		xItem.setImage(this.drugdealerImg);
		xItem.setExpanded(true);
	}
	
	/**
	 * @param parent
	 */
	private void createBody(Composite parent) {
		this.folder = new CTabFolder(parent, SWT.BORDER);
		this.folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
		
		this.folder.setSimple(false);
		this.folder.setUnselectedImageVisible(false);
		this.folder.setUnselectedCloseVisible(false);
		
		this.folder.setSelectionBackground(new Color(parent.getDisplay(), 55, 105, 205));
		this.folder.setSelectionForeground(new Color(parent.getDisplay(), 255, 255, 255));
		this.folder.setFont(new Font(parent.getDisplay(), "Sans", 12, SWT.BOLD));
		
		this.folder.setForeground(new Color(parent.getDisplay(), 255, 255, 255));
		this.folder.setBackground(new Color(parent.getDisplay(), 125, 160, 225));
		this.folder.setBorderVisible(false);
		
		this.folder.addCTabFolder2Listener(new CTabFolder2Adapter() {
			public void close(CTabFolderEvent event) {
				discardTab(false);
				event.doit = false;
			}
		});
	}
	
	/**
	 * @param parent
	 */
	private void createButtons(Composite parent) {
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, false, 2, 1));
		comp.setLayout(new RowLayout());

		this.discardBtn = new Button(comp, SWT.PUSH);
		this.discardBtn.setLayoutData(new RowData(200, 40));
		this.discardBtn.setText("&Descartar");
		this.discardBtn.setImage(this.discardImg);
		this.discardBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (folder.getItemCount() > 0)
					discardTab(false);
			}
		});
		
		this.saveBtn = new Button(comp, SWT.PUSH);
		this.saveBtn.setLayoutData(new RowData(200, 40));
		this.saveBtn.setText("&Salvar");
		this.saveBtn.setImage(this.saveImg);
		this.saveBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (folder.getItemCount() > 0)
					saveRegister();
			}
		});
	}

	/**
	 * 
	 */
	protected abstract void discardTab(boolean yes);
	
	
	/**
	 * @param composite
	 */
	protected void addCadastrarTab(Composite composite, String name) {
		this.addTab(composite, name, false);
	}
	
	/**
	 * @param composite
	 */
	protected void addEditarTab(Composite composite, String name) {
		this.addTab(composite, name, true);
	}
	
	/**
	 *
	 */
	private void addTab(Composite composite, String name, boolean edit) {
		CTabItem item = new CTabItem(this.folder, SWT.CLOSE);
		item.setText(name);
		if (edit)
			item.setImage(this.editImg);
		else
			item.setImage(this.newImg);
		item.setControl(composite);
		this.folder.setSelection(item);
	}

	protected abstract void saveRegister();
	protected abstract void deleteRegister();
	protected abstract void searchRegister(int type);
	protected abstract void openRegister(int type);
	
}
